package org.maylove.wuziqi.service;

import org.maylove.wuziqi.dao.HistoryDao;
import org.maylove.wuziqi.pojo.history.History;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HistoryServiceImpl implements HistoryService {

    @Autowired
    private HistoryDao historyDao;

    @Override
    public void addHistory(Long userId, Integer result, Integer reason, Integer[][] board) {
        History history = new History();
        history.setUserId(userId);
        history.setResult(result);
        history.setReason(reason);
        int row1 = 0;
        for(int i=1;i<=15;i++) {
            if(board[1][i] != 0) row1 |= (1<<(i+14));
            if(board[1][i] == 1) row1 |= (1<<(i-1));
        }
        history.setRow1(row1);
        int row2 = 0;
        for(int i=1;i<=15;i++) {
            if(board[2][i] != 0) row2 |= (1<<(i+14));
            if(board[2][i] == 1) row2 |= (1<<(i-1));
        }
        history.setRow2(row2);
        int row3 = 0;
        for(int i=1;i<=15;i++) {
            if(board[3][i] != 0) row3 |= (1<<(i+14));
            if(board[3][i] == 1) row3 |= (1<<(i-1));
        }
        history.setRow3(row3);
        int row4 = 0;
        for(int i=1;i<=15;i++) {
            if(board[4][i] != 0) row4 |= (1<<(i+14));
            if(board[4][i] == 1) row4 |= (1<<(i-1));
        }
        history.setRow4(row4);
        int row5 = 0;
        for(int i=1;i<=15;i++) {
            if(board[5][i] != 0) row5 |= (1<<(i+14));
            if(board[5][i] == 1) row5 |= (1<<(i-1));
        }
        history.setRow5(row5);
        int row6 = 0;
        for(int i=1;i<=15;i++) {
            if(board[6][i] != 0) row6 |= (1<<(i+14));
            if(board[6][i] == 1) row6 |= (1<<(i-1));
        }
        history.setRow6(row6);
        int row7 = 0;
        for(int i=1;i<=15;i++) {
            if(board[7][i] != 0) row7 |= (1<<(i+14));
            if(board[7][i] == 1) row7 |= (1<<(i-1));
        }
        history.setRow7(row7);
        int row8 = 0;
        for(int i=1;i<=15;i++) {
            if(board[8][i] != 0) row8 |= (1<<(i+14));
            if(board[8][i] == 1) row8 |= (1<<(i-1));
        }
        history.setRow8(row8);
        int row9 = 0;
        for(int i=1;i<=15;i++) {
            if(board[9][i] != 0) row9 |= (1<<(i+14));
            if(board[9][i] == 1) row9 |= (1<<(i-1));
        }
        history.setRow9(row9);
        int row10 = 0;
        for(int i=1;i<=15;i++) {
            if(board[10][i] != 0) row10 |= (1<<(i+14));
            if(board[10][i] == 1) row10 |= (1<<(i-1));
        }
        history.setRow10(row10);
        int row11 = 0;
        for(int i=1;i<=15;i++) {
            if(board[11][i] != 0) row11 |= (1<<(i+14));
            if(board[11][i] == 1) row11 |= (1<<(i-1));
        }
        history.setRow11(row11);
        int row12 = 0;
        for(int i=1;i<=15;i++) {
            if(board[12][i] != 0) row12 |= (1<<(i+14));
            if(board[12][i] == 1) row12 |= (1<<(i-1));
        }
        history.setRow12(row12);
        int row13 = 0;
        for(int i=1;i<=15;i++) {
            if(board[13][i] != 0) row13 |= (1<<(i+14));
            if(board[13][i] == 1) row13 |= (1<<(i-1));
        }
        history.setRow13(row13);
        int row14 = 0;
        for(int i=1;i<=15;i++) {
            if(board[14][i] != 0) row14 |= (1<<(i+14));
            if(board[14][i] == 1) row14 |= (1<<(i-1));
        }
        history.setRow14(row14);
        int row15 = 0;
        for(int i=1;i<=15;i++) {
            if(board[15][i] != 0) row15 |= (1<<(i+14));
            if(board[15][i] == 1) row15 |= (1<<(i-1));
        }
        history.setRow15(row15);
        history.setHistoryId(java.time.Instant.now().toEpochMilli());
        historyDao.save(history);
    }

    @Override
    public List<History> getHistory(Long userId) {
        return historyDao.getHistory(userId);
    }


}
